//
//  Game.m
//  citycar
//
//  Created by Tang JiaQi on 3/15/11.
//  Copyright 2011 HOME. All rights reserved.
//

#import "GameLayer.h"
#import "Car.h"
#import	"Track.h"
#import "CCTouchDispatcher.h"
#import "CarManager.h"
#import "Coordinate.h"

@implementation GameLayer

enum{
	EControlName_WheelLeft,
	EControlName_WheelCentral,
	EControlName_WheelRight,
	EControlName_Acceleration,
	EControlName_DeAcceleration,
	EControlName_Num
};		
static NSString* gsControlHandler[] = {
	@"wheelLeft",
	@"wheelCentral",
	@"wheelRight",
	@"accelerate",
	@"deAccelerate"
};
CCSprite* gControl[EControlName_Num] = {nil};

+(id) sGame
{
	CCScene* mainScene = [CCScene node];
	GameLayer* game = [GameLayer node];
	[mainScene addChild:game];
	
	return game;
}

-(id) init
{
	if((self = [super init]))
	{
		Track* pTrack = [[Track alloc] init];
		[pTrack AddTrackCell:@"Express_road_small.png"];
		[pTrack AddTrackCell:@"Express_road_small.png"];
		[self addChild:pTrack];
		[pTrack Reset];
		
		CGPoint initPos = ccp(0.f, 3.f);
		TsiU::Vec2 initVel = TsiU::Vec2(0.f, 0.f);
		TsiU::Vec2 initOri = TsiU::Vec2(0.f, 1.f);
		initOri.Nomalize();
		
		Car* pCar = [[CarManager sharedCarManager] addCar:ECarRole_User	carType:ECarType_Car1 initPos:initPos initVelocity:initVel initOrientation:initOri];
		[pCar setFocused:YES];
		[self addChild:pCar];
		
		//set focused car
		[pTrack SetFocusCar:pCar];
		
		initPos = ccp(0.f, 5.f);
		initVel = TsiU::Vec2(0.f, 0.f);
		initOri = TsiU::Vec2(0.f, -1.f);
		initOri.Nomalize();
		
		pCar = [[CarManager sharedCarManager] addCar:ECarRole_Opponent carType:ECarType_Car2 initPos:initPos initVelocity:initVel initOrientation:initOri];
		[self addChild:pCar];
		
		[self addChild:[CarManager sharedCarManager]];
		
		//CCSprite *point = [CCSprite spriteWithFile:@"acceleration.png"];
		//point.scale = 0.25f;
		//point.position = [Coordinate realWorldToScreen: pCar.position];
		//[self addChild:point z:10];
		
		CGSize winSize = [[CCDirector sharedDirector] winSize];
		
		for(int i = 0; i < EControlName_Num; ++i)
		{
			gControl[i] = nil;
		}
		gControl[EControlName_WheelLeft] = [CCSprite spriteWithFile: @"left-small.png"];
		gControl[EControlName_WheelLeft].position = ccp(winSize.width / 2.f - gControl[EControlName_WheelLeft].textureRect.size.width - 10.f, 
														1.f + gControl[EControlName_WheelLeft].textureRect.size.height / 2.f);
		gControl[EControlName_WheelRight] = [CCSprite spriteWithFile: @"right-small.png"];
		gControl[EControlName_WheelRight].position = ccp(winSize.width / 2.f + gControl[EControlName_WheelRight].textureRect.size.width + 10.f , 
														 1.f + gControl[EControlName_WheelRight].textureRect.size.height / 2.f);
		gControl[EControlName_Acceleration] = [CCSprite spriteWithFile: @"acceleration.png"];
		gControl[EControlName_Acceleration].position = ccp(winSize.width / 2.f - gControl[EControlName_Acceleration].textureRect.size.width + 8, 
														  16.f + gControl[EControlName_Acceleration].textureRect.size.height / 2.f);
		
		gControl[EControlName_DeAcceleration] = [CCSprite spriteWithFile: @"deacceleration.png"];
		gControl[EControlName_DeAcceleration].position = ccp(winSize.width / 2.f + gControl[EControlName_DeAcceleration].textureRect.size.width - 15, 
															 12.f + gControl[EControlName_DeAcceleration].textureRect.size.height / 2.f);
		
		for(int i = 0; i < EControlName_Num; ++i)
		{
			if(gControl[i] != nil)
				[self addChild:gControl[i]];
		}
		
		self.isTouchEnabled = YES;
	}
	return self;
}

-(void) registerWithTouchDispatcher
{
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
	CGPoint location = [self convertTouchToNodeSpace: touch];
	for(int i = 0; i < EControlName_Num; ++i)
	{
		if(gControl[i] == nil)
			continue;
			
		if(CGRectContainsPoint([gControl[i] boundingBox], location))
		{
			SEL controlHandler = NSSelectorFromString(gsControlHandler[i]);
			Car* userCar = [[CarManager sharedCarManager] getUserCar];
			[userCar performSelector: controlHandler];
		}
	}
	return YES;
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
	Car* userCar = [[CarManager sharedCarManager] getUserCar];
	[userCar wheelCentral];
}

@end
